package com.example.kt_all.ui


import android.util.Log
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.kt_all.R
import com.example.kt_all.adapter.OperationListItemClickListener
import com.example.kt_all.adapter.OperationListRVAdapter
import com.example.kt_all.bean.req.OpListBeanReq
import com.example.kt_all.bean.resp.OpListResp
import com.example.kt_all.databinding.ActivityOperationListBinding
import com.example.kt_all.viewmodel.AllViewModel
import com.example.kt_common.base.BaseActivity
import com.example.kt_common.network.BaseStateObserver
import com.example.kt_common.util.Constants
import com.example.kt_common.util.ToastUtil
import org.koin.androidx.viewmodel.ext.android.viewModel

@Route(path = Constants.PATH_OPERATIONG_LIST)
class OperationListActivity : BaseActivity<ActivityOperationListBinding>() {
    private val allViewModel: AllViewModel by viewModel()

    lateinit var operationListRVAdapter: OperationListRVAdapter
    lateinit var lm: LinearLayoutManager
    var isLoadMore = false
    var paseSize: Int = 5
    var page: Int = 1
    var carNum: String? = ""
    var startTime: String? = ""
    var endTime: String? = ""
    var carNums: MutableList<String> = mutableListOf()
    var list: MutableList<OpListResp.OperationVo> = arrayListOf()


    override fun getLayoutID(): Int {
        return R.layout.activity_operation_list
    }

    override fun init() {
        if (intent == null) {
            return
        }
        carNum = intent.getStringExtra(Constants.carNum)
        startTime = intent.getStringExtra(Constants.startTime)
        endTime = intent.getStringExtra(Constants.endTime)
        allViewModel.opListData.observe(this, object : BaseStateObserver<OpListResp>(null) {
            override fun getRespDataSuccess(it: OpListResp) {
                ToastUtil.showMsg("请求 营运数据列表接口 成功")
                resetUI()
                //下拉刷新
                if (page == 1) {
                    list.clear()
                }
                //最后一页
//                if (it.over) {
//                    operationListRVAdapter.isLastPage = true
//                }

                //list添加数据
                list.addAll(it.operationVoList)

                //处理数据
                if (page == 0) {
                    operationListRVAdapter.setData(null)
                    operationListRVAdapter.setData(list)
                    lm.scrollToPosition(0)
                } else {
                    operationListRVAdapter.setData(list)
                }

                Log.d(TAG, "observe opListData: " + list.size)
            }

            override fun getRespDataEnd() {
                resetUI()
            }
        })
        if (carNum?.isEmpty() == true) {
            return
        }
        carNums = mutableListOf(carNum!!)

        lm = LinearLayoutManager(this)
        mBind.bottomView.layoutManager = lm
        operationListRVAdapter = OperationListRVAdapter(object : OperationListItemClickListener {
            override fun onItemClick(position: Int) {
                val data = list[position]
                ToastUtil.showMsg("position $position")
//                ARouter.getInstance()
//                    .build(Constants.PATH_WEB)
//                    .withString(Constants.WEB_LINK, data.link)
//                    .withString(Constants.WEB_TITLE, data.title)
//                    .navigation()
            }}
            )
                mBind . bottomView . adapter = operationListRVAdapter

                mBind.bottomView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
                    override fun onScrollStateChanged(
                        recyclerView: RecyclerView,
                        newState: Int
                    ) {
                        if (newState == RecyclerView.SCROLL_STATE_IDLE && operationListRVAdapter.itemCount != 0 && (lm.findLastVisibleItemPosition() + 1) == operationListRVAdapter.itemCount && !isLoadMore && !operationListRVAdapter.isLastPage) {
                            Log.d(TAG, "onScrollStateChanged: last-----")
                            isLoadMore = true
                            page += 1
                            getData()
                        }
                    }
                })


                mBind . srlHome . setColorSchemeResources (com.example.kt_common.R.color.theme_color)
                mBind . srlHome . setOnRefreshListener {
            operationListRVAdapter.isLastPage = false
            getData()
        }
                getData ()
    }

    private fun getData() {
        // 创建登录请求体
        val opListBeanReq = OpListBeanReq(carNums, startTime!!, endTime!!, page, paseSize)
        allViewModel.getOpList(opListBeanReq)
    }

    private fun resetUI() {
        isLoadMore = false//加载更多完成，重置false
        if (mBind.srlHome.isRefreshing) {
            mBind.srlHome.isRefreshing = false
        }
    }

}